﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using TransitionEffects;

namespace JizCodeWPF.Sample
{
    /// <summary>
    /// Window1.xaml 的交互逻辑
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private List<UserControl> _views = new List<UserControl>();

        public void Navigate(object view)
        {
            if(_views.Count >0)
            {
                container.Children.Add((UserControl)view);
                ((UserControl)view).Loaded += Window1_Loaded;
            }
            else
            {
                container.Children.Add((UserControl)view);
                _views.Add((UserControl)view);
            }
        }

        void Window1_Loaded(object sender, RoutedEventArgs e)
        {
            ApplyTransitionEffect(true, _views.LastOrDefault(), (UserControl)sender);
            _views.Add((UserControl)sender);
        }

        int seed = 0;
        private void ApplyTransitionEffect(bool isNext, Visual oldVisual, Visual newVisual)
        {
            //canvasEffect.Visibility = System.Windows.Visibility.Visible;
            int width = (int)container.ActualWidth;
            int height = (int)container.ActualHeight;
            Debug.WriteLine(string.Format("{0},{1}", width, height));
            RenderTargetBitmap RenderBmp = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32);
            RenderBmp.Render(newVisual);
            this.currentChild.Source = RenderBmp;

            RenderTargetBitmap RenderBmp2 = new RenderTargetBitmap(width, height, 96, 96, PixelFormats.Pbgra32);
            RenderBmp2.Render(oldVisual);
            this.oldChild.Source = RenderBmp2;

            //if(seed % 2 ==0)
            //{
            //    this.currentChild.Source = new BitmapImage(new Uri("TransitionImages/1.jpg", UriKind.Relative));
            //    this.oldChild.Source = new BitmapImage(new Uri("TransitionImages/2.jpg", UriKind.Relative));
            //}
            //else
            //{
            //    this.currentChild.Source = new BitmapImage(new Uri("TransitionImages/2.jpg", UriKind.Relative));
            //    this.oldChild.Source = new BitmapImage(new Uri("TransitionImages/1.jpg", UriKind.Relative));
            //}
            //seed++;

            this.oldChild.Effect = null;

            TransitionEffect[] effectGroup = new TransitionEffect[]
            {
                new SlideInTransitionEffect{ SlideAmount= new Point(1, 0)},
                new SlideInTransitionEffect{ SlideAmount=new Point(0, 1)},
                new SlideInTransitionEffect{ SlideAmount=new Point(-1, 0)},
                new SlideInTransitionEffect{ SlideAmount=new Point(0, -1)},
            };
            TransitionEffect effect = null;
            if (isNext)
            {
                effect = effectGroup[0];
            }
            else
            {
                effect = effectGroup[2];
            }

            DoubleAnimation da = new DoubleAnimation(0.0, 1.0, new Duration(TimeSpan.FromSeconds(0.8f)), FillBehavior.HoldEnd);
            da.AccelerationRatio = 0.01f;
            da.DecelerationRatio = 0.5f;
            da.Completed += new EventHandler(this.TransitionCompleted);
            effect.BeginAnimation(TransitionEffect.ProgressProperty, da);

            VisualBrush vb = new VisualBrush(this.oldChild);
            vb.Viewbox = new Rect(0, 0, this.oldChild.ActualWidth, this.oldChild.ActualHeight);
            Debug.WriteLine(string.Format("oldchild {0},{1}", this.oldChild.ActualWidth, this.oldChild.ActualHeight));
            vb.ViewboxUnits = BrushMappingMode.Absolute;

            ////this.oldChild.Width = this.oldChild.ActualWidth;
            ////this.oldChild.Height = this.oldChild.ActualHeight;
            ////this.oldChild.Measure(new Size(this.oldChild.ActualWidth, this.oldChild.ActualHeight));
            ////this.oldChild.Arrange(new Rect(0, 0, this.oldChild.ActualWidth, this.oldChild.ActualHeight));

            effect.OldImage = vb;
            this.currentChild.Effect = effect;
        }


        private void TransitionCompleted(object sender, EventArgs e)
        {
            this.currentChild.Effect = null;
            //canvasEffect.Visibility = System.Windows.Visibility.Hidden;

            Debug.WriteLine("switch finish");
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Navigate(new Page1());
            //ApplyTransitionEffect(true);
        }

        private void Button_Click2(object sender, RoutedEventArgs e)
        {
            Navigate(new Page2());
            //ApplyTransitionEffect(false);
        }
    }
}
